package com.llh.controller.processdata;

import com.llh.bean.MiddleData;
import com.llh.mapper.MiddleDataMapper;
import com.llh.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ComputePeriod {
    public static void compute() {
        SqlSession sqlSesion = null;
        try {
            sqlSesion = SqlSessionUtil.getSqlSesion();
            MiddleDataMapper mapper = sqlSesion.getMapper(MiddleDataMapper.class);
            List<MiddleData> middleDataList = mapper.selectAll();
            sqlSesion.close();
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            int num = 0;
            for (MiddleData middleData : middleDataList) {
                sqlSesion = SqlSessionUtil.getSqlSesion();
                mapper = sqlSesion.getMapper(MiddleDataMapper.class);
                String s_code = middleData.getS_code();
                Date open_day = middleData.getOpen_day();
//                Integer period_no = middleData.getPeriod_no();
                String s = format.format(open_day);
                int no = Integer.valueOf(s);
                int year = no / 10000;
                int month = (no % 10000) / 100;
                String per = "";
                if (0 < month && month <= 3) {
                    middleData.setPeriod_no(1);
                }
                if (3 < month && month <= 6) {
                    middleData.setPeriod_no(2);
                }
                if (6 < month && month <= 9) {
                    middleData.setPeriod_no(3);
                }
                if (9 < month && month <= 12) {
                    middleData.setPeriod_no(4);
                }
                Integer period_no = middleData.getPeriod_no();
                switch (period_no) {
                    case 1:
                        //一季度往前减一年
                        year -= 1;
                        per = year + "1231";
                        break;
                    case 2:
                        per = year + "0331";
                        break;
                    case 3:
                        per = year + "0630";
                        break;
                    case 4:
                        per = year + "0930";
                        break;
                }

                Date parse = format.parse(per);
                middleData.setPeriod(parse);
                mapper.setPeriod(middleData);
                sqlSesion.close();
                System.out.println("第" + (++num) + "次");
            }
//            mapper.setPeriod(middleDataList);
            System.out.println("报告日期计算成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错终止");
        } finally {

        }
    }

    public static void cpmputeExchange() {
        SqlSession sqlSession = null;
        List<MiddleData> middleDataList = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSesion();
            MiddleDataMapper mapper = sqlSession.getMapper(MiddleDataMapper.class);
            middleDataList = mapper.selectAll();

        } finally {
            sqlSession.close();
        }
        int num = 0;
        if (middleDataList != null || middleDataList.size() > 0) {
            for (MiddleData middleData : middleDataList) {
                String s_code = middleData.getS_code();
                if (s_code.endsWith(".SH")) {
                    middleData.setExchange("SSE");
                } else if (s_code.endsWith(".SZ")) {
                    //深交所
                    middleData.setExchange("SZSE");
                }
                SqlSession sqlSesion = SqlSessionUtil.getSqlSesion();
                MiddleDataMapper mapper = sqlSesion.getMapper(MiddleDataMapper.class);
                mapper.updateExchange(middleData);
                sqlSesion.close();
                System.out.println("第" + (++num) + "次");
            }
            System.out.println("成功结束");
        }


    }

    public static void cpmputeDuration() {
        SqlSession sqlSession = null;
        List<MiddleData> middleDataList = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSesion();
            MiddleDataMapper mapper = sqlSession.getMapper(MiddleDataMapper.class);
            middleDataList = mapper.selectAll();

        } finally {
            sqlSession.close();
        }
        int num = 0;
        if (middleDataList != null || middleDataList.size() > 0) {
            for (MiddleData middleData : middleDataList) {
                SqlSession sqlSesion = SqlSessionUtil.getSqlSesion();
                MiddleDataMapper mapper = sqlSesion.getMapper(MiddleDataMapper.class);
                mapper.updateDate(middleData);
//                Map<String, Date> stringDateMap = mapper.selectDate(middleData);
//                middleData.setStart_day(stringDateMap.get("start_day"));
//                middleData.setEnd_day(stringDateMap.get("end_day"));
                sqlSesion.close();
                System.out.println("第" + (++num) + "次修改");
            }
//            num = 0;
//            for (MiddleData middleData : middleDataList) {
//                SqlSession sqlSesion = SqlSessionUtil.getSqlSesion();
//                MiddleDataMapper mapper = sqlSesion.getMapper(MiddleDataMapper.class);
//                mapper.updateday(middleData);
//                sqlSesion.close();
//                System.out.println("第" + (++num) + "次修改");
//            }

            System.out.println("区间计算完毕");

        }
    }
    public static void main(String[] args) {
//        compute();
//        cpmputeExchange();
        cpmputeDuration();
    }
}
